/**
 * 表单格式化相关工具，用于保存一些常量和数据格式化
 * 用法：
 * import times from '../../../Resources/utils/wt-form-utils'
 */

const PageIndexParam = 'index';
const PageSizeParam = 'number';
const DefaultPageSize = 100;

export default {
	genderOptions: [
		'MALE',
		'FEMALE'
	],
	genderCode:[
		1,
		0
	],
	genderText: [
		'男',
		'女'
	],
	yesnoText: [
		'是',
		'否'
	],
	getMaleValue: function() {
		return this.genderOptions[0];
	},
	getFemaleValue: function() {
		return this.genderOptions[1];
	},
	formatGender: function(gender) {
		for (let i = 0; i < this.genderOptions.length; i ++) {
			if (gender == this.genderOptions[i] || gender == this.genderCode[i]) {
				return this.genderText[i];
			}
		}
		return gender;
	},
	formatYesNo: function(yesno) {
		if (yesno == 0) {
			return yesnoText[0];
		}
		else if (yesno == 1) {
			return yesnoText[1];
		}
		else if (yesno == 'Y' || yesno == 'y') {
			return yesnoText[0];
		}
		else if (yesno == 'N' || yesno == 'n') {
			return yesnoText[1];
		}
		else {
			return yesno;
		}
	},
	/**
	 * 给参数集合添加统一的分页参数
	 */
	addPaginationParams: function(params, pageIndex = 0, pageSize = DefaultPageSize) {
		params[PageIndexParam] = pageIndex;
		params[PageSizeParam] = pageSize;
	},
	/**
	 * 从服务器查询结果中解析数组对象
	 * @param {String,Array} result 
	 */
	parseArray(result) {
		if (result instanceof Array) {
			return result;
		}
		else if (typeof result == 'string') {
			if (result != '') {
				return JSON.parse(result);
			}
			else {
				return [];
			}
		}
		return null;
	},
	parseObject(result) {
		if (result instanceof Object || result instanceof Array) {
			return result;
		}
		else if (typeof result == 'string') {
			return JSON.parse(result);
		}
		return null;
	}
};